package the_third_chapter;

import java.util.Scanner;

public class Factorial {

	// 计算数字的阶乘
	public static int calFactorial(int num) {
		if (num < 0) {
			throw new IllegalArgumentException("输入不能为负数");
		}
		if (num == 0) {
			return 1;
		}
		// return num * calFactorial(num - 1);
		int sum = calFactorial(num - 1);
		sum = sum * num;
		return sum;
	}

	// 计算从0到此数字的阶乘和
	public static int calFactorialSum(int num) {
		int sum = 0;
		for (int i = 1; i <= num; i++) {
			sum = sum + calFactorial(i);
		}
		return sum;
	}

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个非负数：");
		int num = input.nextInt();
		System.out.println("阶乘结果为：" + calFactorial(num));
		System.out.println("阶乘和为：" + calFactorialSum(num));
	}
}
